package com.comdo.newup_loan.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.module.home.SuggestionActivity;

public class TitleMenuUtil {
	private AppCompatActivity activity;
	private String titleStr;
	private TextView title;
	private ImageView suggestImageView;

	public TitleMenuUtil(AppCompatActivity activity, String titleStr,Boolean isRightShow) {
		this.activity = activity;
		this.titleStr = titleStr;
		init(isRightShow);
	}

	public void init(Boolean isRightShow) {
		title = (TextView) activity.findViewById(R.id.title);
		title.setText(titleStr);
		suggestImageView=(ImageView) activity.findViewById(R.id.img_suggest);
		if(isRightShow)
			suggestImageView.setVisibility(View.VISIBLE);
		suggestImageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(activity, SuggestionActivity.class);
				activity.startActivity(intent);
			}
		});
		Toolbar toolbar = (Toolbar)activity.findViewById(R.id.toolbar);
		toolbar.setTitle("");
		toolbar.setNavigationIcon(R.drawable.icon_back);
		activity.setSupportActionBar(toolbar);
		//关键下面两句话，设置了回退按钮，及点击事件的效果
		activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		toolbar.setNavigationOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				activity.finish();
			}
		});
	}

	/**
	 * 隐藏软键盘
	 * 
	 * @param activity
	 */
	public void hideSoftKeyboard(Activity activity) {
		View view = activity.getWindow().peekDecorView();
		if (view != null) {
			InputMethodManager inputmanger = (InputMethodManager) activity
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}
}
